package com.simple.piger.handle;

import com.simple.piger.constant.AjaxCode;
import com.simple.piger.utils.AjaxResult;
import com.simple.piger.utils.DingTalk;
import com.simple.piger.utils.GsonHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author baolw
 */
@ControllerAdvice
@Slf4j
public class HandleExceptionController {
    @Autowired
    private DingTalk dingTalk;

    /**
     * 返回的Map对象会被@ResponseBody注解转换为JSON数据返回
     *
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(HttpServletRequest request, Exception e) {
        dingTalk.sendException(e);
        log.error("服务异常。参数="+GsonHelper.toJson(request.getParameterMap()), e);
        return new AjaxResult(AjaxCode.INTERNAL_SERVER_ERROR);
    }
}
